/**
 * 
 */
package com.pxingg.db;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.pxingg.exception.XException;
import com.pxingg.log.LogConfig;

/**
 * @author pxingg
 * 
 */
public class XDBTest
{
    /**
     * The log of this class
     */
    static Log log = LogFactory.getLog(XDBTest.class);

    /**
     * @throws java.lang.Exception
     */
    @BeforeClass
    public static void setUpBeforeClass() throws Exception
    {
        LogConfig.init();
    }

    /**
     * @throws java.lang.Exception
     */
    @AfterClass
    public static void tearDownAfterClass() throws Exception
    {
    }

    /**
     * @throws java.lang.Exception
     */
    @Before
    public void setUp() throws Exception
    {
    }

    /**
     * @throws java.lang.Exception
     */
    @After
    public void tearDown() throws Exception
    {
    }

    /**
     * Test method for {@link com.pxingg.db.XDB#queryOneRecord(java.lang.String)}.
     */
    @Test
    public final void testQueryOneRecord()
    {
        try
        {
            final XDB xdb = XDB.getInstance();
            xdb.update("drop table if exists people;");
            xdb.update("create table people (name, occupation)");

            final String[] vaules = new String[2];
            vaules[0] = "puxing";
            vaules[1] = "huawei";
            xdb.update("insert into people values(?, ?)", vaules);

            final String selectResult[] = xdb.queryOneRecord("select name, occupation from people");

            Assert.assertTrue(vaules[0].equals(selectResult[0]));
            Assert.assertTrue(vaules[1].equals(selectResult[1]));
        }
        catch (final XException e)
        {
            e.printStackTrace();
            Assert.fail("failed when test testUpdateString");
        }
    }

    /**
     * Test method for {@link com.pxingg.db.XDB#queryRecord(java.lang.String)}.
     */
    @Test
    public final void testQueryRecord()
    {
        try
        {
            final XDB xdb = XDB.getInstance();;
            xdb.update("drop table if exists people");
            xdb.update("create table people (name, occupation)");
            final String[][] vaules = new String[2][2];
            vaules[0][0] = "puxing";
            vaules[0][1] = "huawei";
            vaules[1][0] = "kongkong";
            vaules[1][1] = "ganghua";
            xdb.update("insert into people values(?, ?)", vaules);

            final String selectResult[][] = xdb.queryRecord("select name, occupation from people");

            Assert.assertTrue(vaules[0][0].equals(selectResult[0][0]));
            Assert.assertTrue(vaules[0][1].equals(selectResult[0][1]));
            Assert.assertTrue(vaules[1][0].equals(selectResult[1][0]));
            Assert.assertTrue(vaules[1][1].equals(selectResult[1][1]));
        }
        catch (final XException e)
        {
            e.printStackTrace();
            Assert.fail("failed when test testUpdateStringStringArray");
        }
    }

    /**
     * Test method for {@link com.pxingg.db.XDB#update(java.lang.String)}.
     */
    @Test
    public final void testUpdateString()
    {

        try
        {
            final XDB xdb = XDB.getInstance();
            xdb.update("drop table if exists people;");
            xdb.update("create table people (name, occupation)");
            xdb.update("insert into people values('puxing', 'huawei')");
        }
        catch (final XException e)
        {
            e.printStackTrace();
            Assert.fail("failed when test testUpdateString");
        }

    }

    /**
     * Test method for {@link com.pxingg.db.XDB#update(java.lang.String[])}.
     */
    @Test
    public final void testUpdateStringArray()
    {

        try
        {
            final XDB xdb = XDB.getInstance();
            xdb.update("drop table if exists people;");
            xdb.update("create table people (name, occupation)");
            final String[] sqls = new String[1000];
            for(int i = 0; i < sqls.length; i++)
            {
                sqls[i] = "insert into people values('puxing', 'huawei" + i + "')";
            }

            xdb.update(sqls);

            // 开始测试包含空字符串的情况
            for(int i = 0; i < sqls.length; i++)
            {
                sqls[i] = null;
            }
        }
        catch (final XException e)
        {
            e.printStackTrace();
            Assert.fail("failed when test testUpdateString");
        }

    }

    /**
     * Test method for {@link com.pxingg.db.XDB#update(java.lang.String, java.lang.String[])}.
     */
    @Test
    public final void testUpdateStringStringArray()
    {
        try
        {
            final XDB xdb = XDB.getInstance();
            xdb.update("drop table if exists people");
            xdb.update("create table people (name, occupation)");
            final String[] vaules = new String[2];
            vaules[0] = "puxing";
            vaules[1] = "huawei";
            xdb.update("insert into people values(?, ?)", vaules);
        }
        catch (final XException e)
        {
            e.printStackTrace();
            Assert.fail("failed when test testUpdateStringStringArray");
        }
    }

    /**
     * Test method for {@link com.pxingg.db.XDB#update(java.lang.String, java.lang.String[][])}.
     */
    @Test
    public final void testUpdateStringStringArrayArray()
    {
        try
        {
            final XDB xdb = XDB.getInstance();
            xdb.update("drop table if exists people");
            xdb.update("create table people (name, occupation)");
            final String[][] vaules = new String[2][2];
            vaules[0][0] = "puxing";
            vaules[0][1] = "huawei";
            vaules[1][0] = "kongkong";
            vaules[1][1] = "ganghua";
            xdb.update("insert into people values(?, ?)", vaules);

            final String result[] = xdb.queryOneRecord("select count(*) from people");
            Assert.assertEquals("2", result[0]);
        }
        catch (final XException e)
        {
            e.printStackTrace();
            Assert.fail("failed when test testUpdateStringStringArray");
        }
    }
}
